<?php
header("Content-Type: text/html;charset=utf-8");
use think\Db;
//redis
$redis = new \Redis;
$redis->connect("127.0.0.1","6379");
//这个操作就是告诉redis扩展，当执行scan命令后，返回的结果集为空的话，函数不返回，而是直接继续执行scan命令，当然，这
//些步骤都是由扩展自动完成，当scan函数返回的时候，要么返回false，即迭代结束，未发现匹配模式pattern的key，要么就返回匹
//配的key，而不再会返回空数组了。
$redis->setOption(Redis::OPT_SCAN,Redis::SCAN_RETRY);

//mysql
$conn = new mysqli("127.0.0.1","root","","noteredis2");
if($conn->connect_error) {
    die("连接失败".$conn->connect_error);
}

$conn->query("truncate table tp_note;");

$cursor = null;
while($keys = $redis->scan($cursor,"hash-*")) {
    foreach($keys as $key) {
        //dump($key);
        $iterator = null;
        $data = $redis->hScan($key,$iterator);
        //Db::table("tp_note")->insert($data);
        $noteName = $data["note_name"];
        $noteText = $data["note_text"];
        $userName = $data["user_name"];
        $noteDatetime = $data["note_datetime"];
        $sql = "INSERT INTO tp_note (note_name, note_text, user_name,note_datetime) VALUES ('$noteName', '$noteText', '$userName','$noteDatetime')";
//加了这句就成了
$conn->query("set names 'utf8'");
if($conn->query($sql)==true) {
}
else {
echo "insert fail";
}
}
}
echo "success";

?>

